import request from "umi-request";

//文件导出
const fileExport = (url, data, title, method = "POST") => {
    const token = localStorage.getItem("Token");
    return new Promise((resolve, reject) => {
        request({
            method,
            url,
            data,
            responseType: "blob",
            headers: {
                "Content-Type": "application/json;charset=UTF-8",
                Authorization: `Basic ${token}`,
            },
        })
            .then((res) => {
                resolve({ code: 200 });
                let url = URL.createObjectURL(res.data);
                var a = document.createElement("a");
                a.href = url; // 给a标签赋上下载地址
                a.download = `${title || "下载文件.xls"}`;
                a.style.display = "none"; // 让a标签不显示
                a.click(); // a标签自点击
                URL.revokeObjectURL(a.href);
            })
            .catch((e) => {
                resolve({ code: 202 });
            });
    });
};

export default fileExport;
